#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import MySQLdb
import sys
#import numpy as np

dbName = "XZDQ-DB-T"
# dbName = "XZDQ-DB"
dbPwd = "cqchc123"
# dbPwd = "123456"
# dbHost = 'localhost'
dbHost = '10.192.19.30'
# dbHost = '39.108.113.79'
# dbHost = '123.56.25.116'
# dbHost = "192.168.112.129"
dbUser = 'root'

#功能：MYSQL数据库查询命令
#输出参数：HOST为IP地址,Name用户名,passwd密码,DBName为数据库名,Str为查询命令
#返回值：row
def SelectMySql(Str):
    try:
        conn = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPwd, db=dbName, port=3306, charset='utf8')
        cursor = conn.cursor()
        cursor.execute(Str)
        row = cursor.fetchall()
        cursor.close
        conn.close
        return row
    except Exception as e:
        return e

#功能：MYSQL数据库插入、删除、更新命令
#输出参数：HOST为IP地址,Name用户名,passwd密码,DBName为数据库名,Str为查询命令
#返回值：row
def CommitMySql(Str):
    try:
        conn = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPwd, db=dbName, port=3306, charset='utf8')
        cursor = conn.cursor()
        cursor.execute(Str)
        conn.commit()
        cursor.close
        conn.close
        return "ok"
    except Exception as e:
        return e
#功能：MYSQL执行存储过程
#输出参数：HOST为IP地址,Name用户名,passwd密码,DBName为数据库名,Str为查询命令
#返回值：row
def ProdMySql(Str):
    try:
        conn = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPwd, db=dbName, port=3306, charset='utf8')
        cursor = conn.cursor()
        cursor.callproc(Str)
        row = cursor.fetchall()
        cursor.close
        conn.close
        return row
    except Exception as e:
        return e
